package com.kordar.admin.modular.sys.service.setting;

import com.kordar.admin.modular.sys.entity.SettingEntity;

import java.util.*;

public class SettingGenerateTree {
    /**
     * array
     * levelTree = [
     * LEVEL_0 => [ITEM_0, ITEM_1, ITEM_2, ...],
     * LEVEL_1 => [ITEM_0, ITEM_1, ITEM_2, ...],
     * LEVEL_2 => [ITEM_0, ITEM_1, ITEM_2, ...],
     * ......
     * ]
     */

    HashMap<Integer, LinkedList<SettingEntity>> groups = new HashMap<>();

    Map<String, Object> map = new LinkedHashMap<>();

    public SettingGenerateTree() {
    }

    private void generateGroups(List<SettingEntity> data) {
        for (SettingEntity item : data) {
            Integer key = item.getPid();
            LinkedList<SettingEntity> ll = this.groups.get(key);
            if (ll == null) {
                ll = new LinkedList<>();
            }
            ll.addLast(item);
            this.groups.put(key, ll);
        }
    }

    public Map<String, Object> treeMap(List<SettingEntity> data, Integer rootKey) {
        this.generateGroups(data);
        if (this.groups.containsKey(rootKey)) {
            LinkedList<SettingEntity> nodes = this.groups.get(rootKey);
            this.generateTree(nodes, 0);
        }
        return this.map;
    }

    private void generateTree(LinkedList<SettingEntity> nodes, int dep) {
        int index = dep + 1;
        for (SettingEntity setting : nodes) {
            Integer id = setting.getId();
            this.map.put(String.valueOf(id), setting);
            if (this.groups.containsKey(id)) {
                this.generateTree(this.groups.get(id), index);
            }
        }
    }

    public List<Object> childTreeMap(List<SettingEntity> data, Integer rootKey, boolean isNone) {
        this.generateGroups(data);
        List<Object> list = new ArrayList<>();
        if (isNone) {
            Map<String, Object> none = new HashMap<>();
            none.put("id", 0);
            none.put("title", "无");
            none.put("level", 1);
            list.add(none);
        }
        if (this.groups.containsKey(rootKey)) {
            LinkedList<SettingEntity> group = this.groups.get(rootKey);
            for (SettingEntity setting : group) {
                Map<String, Object> node = new HashMap<>();
                int id = setting.getId();
                node.put("id", id);
                node.put("title", setting.getTitle());
                node.put("isLast", false);
                node.put("pid", setting.getPid());
                node.put("level", 1);
                if (this.groups.containsKey(id)) {
                    this.generateChildTree(node, id, 1);
                } else {
                    node.put("isLast", true);
                }
                list.add(node);
            }
        }
        return list;
    }

    private void generateChildTree(Map<String, Object> node, int key, int dep) {
        int index = dep + 1;
        List<Object> list = new ArrayList<>();
        LinkedList<SettingEntity> group = this.groups.get(key);
        for (SettingEntity setting : group) {
            int id = setting.getId();
            Map<String, Object> n = new HashMap<>();
            n.put("id", id);
            n.put("title", setting.getTitle());
            n.put("isLast", false);
            n.put("pid", setting.getPid());
            n.put("level", 1);
            if (this.groups.containsKey(id)) {
                this.generateChildTree(n, id, index);
            } else {
                n.put("isLast", true);
            }
            list.add(n);
        }
        node.put("children", list);
    }

}
